window.onload = function() {
    var settlement = [{
        "imgPath": "https://img.alicdn.com/imgextra/i1/883737303/O1CN01nRns4c23op5JM68eV_!!0-item_pic.jpg_430x430q90.jpg",
        "title": "【下单享24期免息】vivo X50双模5G智能手机官方旗舰店官网正品vivoX50全新限量版x30 x27 s6 s5 x50pro",
        "price": "3498",
        "num": "2",
        "sum": "6996"
    }, {
        "imgPath": "https://img.alicdn.com/imgextra/i1/268451883/O1CN01ligkDq1PmSPNVoc2q_!!268451883.jpg_430x430q90.jpg",
        "title": "到手2998起/12期分期/苹果7plus Apple/苹果 iPhone 7 Plus官方旗舰店4G全网通手机苹果11 6P 8 p xr x se 7P",
        "price": "2998",
        "num": "1",
        "sum": "2998"
    }];
    localStorage.setItem('settlement', JSON.stringify(settlement));

    var settlement = JSON.parse(localStorage.getItem('settlement'));
    var order = [];
    var count = 0;
    $.get('data/goods.json', function(response) {
        //获取数据
        for (key in response.goods) {
            for (let i = 0; i < settlement.length; i++) {
                if (response.goods[key].title == settlement[i].title) {
                    order[count] = response.goods[key];
                    order[count].num = settlement[i].num;
                    order[count].sum = settlement[i].sum;
                    order[count].orderNum = new Date().getTime() + settlement[i].price + settlement[i].num;
                    order[count].state = "待发货";
                    count++;
                }
            }
        }
        console.log(order);
        localStorage.setItem('order', JSON.stringify(order)); //将order数组值变成json格式的字符串，作为value存给key

        //填充数据
        var str = ``;
        for (var i = 0; i < order.length; i++) {
            str += `
            <div class="good">
            <ul class="goods_nav">
                <li><input type="checkbox" name="" id="" class="check"></li>
                <li><span>订单号：</span><span class="orderNum">${order[i].orderNum}</span></li>
                <li><span class="shop">${order[i].shop}</span></li>
                <li><span class="del">删除</span></li>
            </ul>
            <ul class="goods_mes">
                <li><span class="img"><img src="${order[i].imgPath}" alt=""></span><span class="title">${order[i].title}</span></li>
                <li><span class="price">${order[i].price}</span></li>
                <li><span class="num">${order[i].num}</span></li>
                <li><span class="sum">${order[i].sum}</span></li>
                <li><span class="state">${order[i].state}</span></li>
                <li><span><button>确认收货</button></span></li>
            </ul>
        </div>`;
        }
        var orderBody = document.querySelector('.orderBody');
        orderBody.innerHTML = str;

        //点击删除按钮，删除所在订单
        var delIndex = -1; //需要删除的行的索引
        $('.del').click(function() {
            delIndex = $('.del').index($(this));
            // console.log(delIndex);
            this.parentNode.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode.parentNode);
            order.splice(delIndex, 1);
            localStorage.setItem('order', JSON.stringify(order));
            // console.log(order);
        })

        //删除选中商品
        var delAll = document.querySelector('.delAll'); //删除选中的按钮
        delAll.onclick = function() {
            var goods = $(".check:checked");
            var len = goods.length;
            for (var i = 0; i < len; i++) {
                var row = goods[i].parentNode.parentNode;
                row.querySelector('.del').click();
            }
            console.log(order);
        }
    })
}